import { Module } from '@nestjs/common';
import { EmailService } from './email.service';
import { MailerModule } from '@nestjs-modules/mailer';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    MailerModule.forRootAsync({
      imports: [ConfigModule],
      async useFactory(configService: ConfigService) {
        const addresser = configService.get<string>('EMAIL_USER');
        return {
          transport: {
            host: configService.get<string>('EMAIL_HOST'),
            port: configService.get<number>('EMAIL_PORT'),
            secure: configService.get('EMAIL_SECURE') === 'true',
            auth: {
              user: addresser,
              pass: configService.get<string>('EMAIL_PASS'),
            },
          },
          defaults: {
            from: addresser,
          },
        };
      },
      inject: [ConfigService],
    }),
  ],
  providers: [EmailService],
  exports: [EmailService],
})
export class EmailModule {}
